package com.edu.study.base.StuTestScoreLevel.service.impl;

import com.edu.study.base.StuTestScoreLevel.domain.StuTestScoreLevel;
import com.edu.study.base.StuTestScoreLevel.mapper.StuTestScoreLevelMapper;
import com.edu.study.base.StuTestScoreLevel.service.IStuTestScoreLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生能力测试得分对应等级关系Service业务层处理
 *
 * @author zw
 * @date 2021-08-05
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuTestScoreLevelServiceImpl implements IStuTestScoreLevelService {
    @Autowired
    private StuTestScoreLevelMapper stuTestScoreLevelMapper;

    /**
     * 查询学生能力测试得分对应等级关系
     *
     * @param id 学生能力测试得分对应等级关系ID
     * @return 学生能力测试得分对应等级关系
     */
    @Override
    public StuTestScoreLevel selectStuTestScoreLevelById(Long id) {
        return stuTestScoreLevelMapper.selectStuTestScoreLevelById(id);
    }

    /**
     * 查询学生能力测试得分对应等级关系列表
     *
     * @param stuTestScoreLevel 学生能力测试得分对应等级关系
     * @return 学生能力测试得分对应等级关系
     */
    @Override
    public List<StuTestScoreLevel> selectStuTestScoreLevelList(StuTestScoreLevel stuTestScoreLevel) {
        return stuTestScoreLevelMapper.selectStuTestScoreLevelList(stuTestScoreLevel);
    }

    /**
     * 新增学生能力测试得分对应等级关系
     *
     * @param stuTestScoreLevel 学生能力测试得分对应等级关系
     * @return 结果
     */
    @Override
    public int insertStuTestScoreLevel(StuTestScoreLevel stuTestScoreLevel) {
        return stuTestScoreLevelMapper.insertStuTestScoreLevel(stuTestScoreLevel);
    }

    /**
     * 修改学生能力测试得分对应等级关系
     *
     * @param stuTestScoreLevel 学生能力测试得分对应等级关系
     * @return 结果
     */
    @Override
    public int updateStuTestScoreLevel(StuTestScoreLevel stuTestScoreLevel) {
        return stuTestScoreLevelMapper.updateStuTestScoreLevel(stuTestScoreLevel);
    }

    /**
     * 批量删除学生能力测试得分对应等级关系
     *
     * @param ids 需要删除的学生能力测试得分对应等级关系ID
     * @return 结果
     */
    @Override
    public int deleteStuTestScoreLevelByIds(Long[] ids) {
        return stuTestScoreLevelMapper.deleteStuTestScoreLevelByIds(ids);
    }

    /**
     * 删除学生能力测试得分对应等级关系信息
     *
     * @param id 学生能力测试得分对应等级关系ID
     * @return 结果
     */
    @Override
    public int deleteStuTestScoreLevelById(Long id) {
        return stuTestScoreLevelMapper.deleteStuTestScoreLevelById(id);
    }

    /**
     * 根据分数获取阶梯解锁等级
     *
     * @param score
     * @return
     */
    @Override
    public int getLevelByScore(Double score) {
        int level = 0;
        StuTestScoreLevel stuTestScoreLevel = new StuTestScoreLevel();
        List<StuTestScoreLevel> levelList = stuTestScoreLevelMapper.selectStuTestScoreLevelList(stuTestScoreLevel);
        for (StuTestScoreLevel s : levelList) {
            double min_value = s.getMinValue();
            double max_value = s.getMaxValue();
            if(score>min_value && score<=max_value){
                level = Integer.parseInt(s.getLevelId());
            }
        }
        return level;
    }
}
